#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''

from pycommon import TBaseFactory
import Bill
import time

class BillFactory(TBaseFactory.TBaseFactory):

    def __init__(self):
        TBaseFactory.TBaseFactory.__init__(self)

    def createClient(self, pid, addr):
        return Bill.BillSrv(pid, addr)

    def checkClients(self, clients):
        curtime = time.time()
        for c in clients:
            c.sendKeepAlive()
            if c.getState()==Bill.BILL_STATE_TO_CLOSE and curtime-c.toclosetime > 10:
                c.abort()
            elif curtime-c.getLastActiveTime() > 60:
                c.abort()

    def getBillSrvCountBySid(self, sid):
        ret = 0
        clients = self.getClients()
        for c in clients:
            if c.getSId()==sid:
                ret += 1
        return ret

    def getBillSrvBySid(self, sid):
        clients = self.getClients()
        for c in clients:
            if c.getSId() == sid:
                return c
        return None

instance = BillFactory()

if __name__ == "__main__":
    pass